sampler imageMap;
int width, height;
float4 PixelShaderMain(float2 texcoord : TEXCOORD0) : COLOR0
{
  const int areaWidth = 1;
  const int areaHeight = 9;
  float4 values[areaWidth * areaHeight];
  float deltaX = 0.5 / width;
  float deltaY = 0.5 / height;
  for(int i = 0; i < areaHeight; i++)
  {
    for(int j = 0; j < areaWidth; j++)
    {
       values[i * areaWidth + j] = tex2D(imageMap, float2(texcoord.x + deltaX * (j - (areaWidth / 2)), texcoord.y - deltaY * (i - (areaHeight / 2))));
	}
  }
  for(int i = 0; i < areaWidth * areaHeight; i++)
  {
    for(int j = 0; j < (areaWidth * areaHeight) - 1 - i; j++)
    {
		float gray1 = (values[j].x + values[j].y + values[j].z) / 3.0f;
		float gray2 = (values[j + 1].x + values[j + 1].y + values[j + 1].z) / 3.0f;

		if (gray1 > gray2)
		{
			float4 value = values[j];
			values[j] = values[j + 1];
			values[j + 1] = value; 
		}
	}
  }
  return values[areaWidth * areaHeight / 2];
}
technique Median
{
  pass Pass_0
  {
    PixelShader = compile ps_2_b PixelShaderMain();
  }
}